/**
 * ISWE.
 * Created on : 22/07/2009
 *
 * Copyright (c) 2009 ISWE.
 * Rafael Noronha de Almeida
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of Rafael Noronha de Almeida
 * ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Rafael.
 */
package br.com.iswe.recipemaster.gwt.security.client;

import br.com.iswe.gwt.ui.client.BeanMap;
import br.com.iswe.gwt.ui.client.MessagePanel;
import br.com.iswe.gwt.ui.client.RPCService;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;

/**
 *
 * Description: 
 *
 * @author Rafael Noronha de Almeida
 *
 * @version $Revision: 1.1 $ $Date: 2009/07/22 09:04:55 $
 *
 */
public class AdminScreen extends Composite {
	private TextBox emailTextBox;

	public AdminScreen() {
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		
		Label lblAdminConsole = new Label("Admin console : ");
		absolutePanel.add(lblAdminConsole, 5, 5);
		
		Button createUserButton = new Button("New button");
		createUserButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				createAdminUser();
			}
		});
		absolutePanel.add(createUserButton, 121, 185);
		createUserButton.setText("Create Admin User");
		
		Label lblGoogleEmail = new Label("Google email : ");
		absolutePanel.add(lblGoogleEmail, 24, 132);
		lblGoogleEmail.setSize("143px", "21px");
		
		emailTextBox = new TextBox();
		absolutePanel.add(emailTextBox, 139, 131);
		emailTextBox.setSize("307px", "22px");
	}
	
	private void createAdminUser(){
		
		RPCService service = new RPCService("mainModuleController");
		
		String emailUser = emailTextBox.getText();
		
		BeanMap beamMap = new BeanMap();
		beamMap.putString("emailUser", emailUser);
		
		service.executeMap("createAdminUser", beamMap, new AsyncCallback<BeanMap>(){

			public void onFailure(Throwable caught) {
				MessagePanel.getInstance().showError(caught, "Error creating the admin user !!!");
			}

			@Override
			public void onSuccess(BeanMap result) {
				MessagePanel.getInstance().showMessage("Admin user created !!!");
			}});
		
	}
}
